package com.ruoyi.ucar.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.ucar.domain.TCustomer;
import com.ruoyi.ucar.domain.po.CustomerDetailMobileQueryPo;
import com.ruoyi.ucar.domain.po.CustomerDetailQueryPo;
import com.ruoyi.ucar.domain.po.CustomerListQueryPo;
import com.ruoyi.ucar.domain.po.CustomerSignQueryPo;
import com.ruoyi.ucar.moreq.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.HashMap;
import java.util.List;

/**
 * 客户主Mapper接口
 * 
 * @author sungangbin
 * @date 2025-05-13
 */
@Mapper
public interface TCustomerMapper extends BaseMapper<TCustomer>
{
    /**
     * 查询客户主
     * 
     * @param id 客户主主键
     * @return 客户主
     */
    public TCustomer selectTCustomerById(Long id);

    /**
     * 查询客户主列表
     * 
     * @param tCustomer 客户主
     * @return 客户主集合
     */
    public List<TCustomer> selectTCustomerList(TCustomer tCustomer);

    /**
     * 新增客户主
     * 
     * @param tCustomer 客户主
     * @return 结果
     */
    public int insertTCustomer(TCustomer tCustomer);

    /**
     * 修改客户主
     * 
     * @param tCustomer 客户主
     * @return 结果
     */
    public int updateTCustomer(TCustomer tCustomer);

    /**
     * 删除客户主
     * 
     * @param id 客户主主键
     * @return 结果
     */
    public int deleteTCustomerById(Long id);

    /**
     * 批量删除客户主
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteTCustomerByIds(Long[] ids);

    @Select("<script>" +
            " SELECT  " +
            " c.id as id, " +
            " c.customer_name as customerName, " +
            " c.customer_uuid as customerUuid, " +
            " c.customer_phone as customerPhone, " +
            " c.customer_wechat as customerWechat, " +
            " c.register_datetime as registerDatetime, " +
            " c.customer_type as customerType, " +
            " c.parent_customer_uuid as parentCustomerUuid, " +
            " pc.customer_name as parentCustomerName, " +
            " pc.agent_grade as parentAgentGrade, " +
            " c.balance_number as balanceNumber, " +
            " c.gift_money as giftMoney, " +
            " c.reward_flag as rewardFlag, " +
            " c.share_code as shareCode, " +
            " c.remark as remark, " +
            " c.report_unread_number as reportUnreadNumber, " +
            " c.avatar_url as avatarUrl, " +
            " c.agent_grade as agentGrade " +
            "FROM " +
            " t_customer c " +
            " LEFT JOIN  t_customer pc ON c.parent_customer_uuid = pc.customer_uuid "+
            "<where>" +
            "   <if test='queryParams.customerName != null and queryParams.customerName != \"\"'> AND c.customer_name LIKE CONCAT('%',#{queryParams.customerName},'%') </if>" +
            "   <if test='queryParams.customerPhone != null and queryParams.customerPhone != \"\"'> AND c.customer_phone LIKE CONCAT('%',#{queryParams.customerPhone},'%') </if>" +
            "   <if test='queryParams.customerWechat != null and queryParams.customerWechat != \"\"'> AND c.customer_wechat LIKE CONCAT('%',#{queryParams.customerWechat},'%') </if>" +
            "   <if test='queryParams.shareCode != null and queryParams.shareCode != \"\"'> AND c.share_code LIKE CONCAT('%',#{queryParams.shareCode},'%') </if>" +
            "   <if test='queryParams.parentCustomerUuid != null and queryParams.parentCustomerUuid != \"\"'> AND c.parent_customer_uuid =#{queryParams.parentCustomerUuid} </if>" +
            "  <if test='queryParams.deleteFlag != null'>" +
            "       AND (c.delete_flag = #{queryParams.deleteFlag} " +
            "       AND (pc.delete_flag = #{queryParams.deleteFlag} or pc.delete_flag is null)) " +
            "   </if>" +
            "   <if test='queryParams.registerBeginDate != null and queryParams.registerEndDate != null'> AND c.register_datetime &gt;= #{queryParams.registerBeginDate} AND c.register_datetime &lt;= #{queryParams.registerEndDate} </if>" +
            "</where>" +
            " order by c.id desc "+
            "</script>")
    public IPage<CustomerListQueryPo> customerListQueryPage(IPage<CustomerListQueryPo> page, @Param("queryParams") CustomerListQueryMoReq  customerListQueryMoReq);


    @Select("<script>" +
            " SELECT  " +
            " c.id as id, " +
            " c.customer_name as customerName, " +
            " c.customer_uuid as customerUuid, " +
            " c.customer_phone as customerPhone, " +
            " c.customer_wechat as customerWechat, " +
            " c.register_datetime as registerDatetime, " +
            " c.customer_type as customerType, " +
            " c.parent_customer_uuid as parentCustomerUuid, " +
            " pc.customer_name as parentCustomerName, " +
            " pc.agent_grade as parentAgentGrade, " +
            " c.balance_number as balanceNumber, " +
            " c.gift_money as giftMoney, " +
            " c.reward_flag as rewardFlag, " +
            " c.share_code as shareCode, " +
            " c.remark as remark, " +
            " c.report_unread_number as reportUnreadNumber, " +
            " c.avatar_url as avatarUrl, " +
            " c.total_agency_fee as totalAgencyFee, " +
            " sd.file_url as signFileUrl, " +
            " c.agent_grade as agentGrade " +

            "FROM " +
            " t_customer c " +
            " LEFT JOIN  t_customer pc ON c.parent_customer_uuid = pc.customer_uuid "+
            " LEFT JOIN  sys_file_data sd ON c.sign_file_main_uuid = sd.file_main_uuid "+
            "<where>" +
            "  <if test='queryParams.deleteFlag != null'>" +
            "       AND (c.delete_flag = #{queryParams.deleteFlag} " +
             "       AND (sd.delete_flag = #{queryParams.deleteFlag} or sd.delete_flag is null)" +
            "       AND (pc.delete_flag = #{queryParams.deleteFlag} or pc.delete_flag is null))" +
            "   </if>" +
            "   <if test='queryParams.id != null'> AND c.id = #{queryParams.id} </if>" +
            "   <if test='queryParams.customerUuid != null and queryParams.customerUuid != \"\"'> AND c.customer_uuid =#{queryParams.customerUuid} </if>" +
            "</where>" +
            "</script>")
    public List<CustomerDetailQueryPo> customerDetailQuery(@Param("queryParams")CustomerDetailQueryMoReq customerDetailQueryMoReq);


    @Select("<script>" +
            " SELECT  " +
            " c.id as id, " +
            " c.customer_name as customerName, " +
            " c.customer_uuid as customerUuid, " +
            " c.customer_phone as customerPhone, " +
            " c.customer_wechat as customerWechat, " +
            " c.register_datetime as registerDatetime, " +
            " c.customer_type as customerType, " +
            " c.parent_customer_uuid as parentCustomerUuid, " +
            " pc.customer_name as parentCustomerName, " +
            " c.balance_number as balanceNumber, " +
            " c.gift_money as giftMoney, " +
            " c.return_money as returnMoney, " +
            " c.total_agency_fee as totalAgencyFee, " +
            " c.reward_flag as rewardFlag, " +
            " c.share_code as shareCode, " +
            " c.remark as remark, " +
            " c.report_unread_number as reportUnreadNumber, " +
            " c.avatar_url as avatarUrl, " +
            " sd.file_url as signFileUrl, " +
            " c.agent_grade as agentGrade " +

            "FROM " +
            " t_customer c " +
            " LEFT JOIN  t_customer pc ON c.parent_customer_uuid = pc.customer_uuid "+
            " LEFT JOIN  sys_file_data sd ON c.sign_file_main_uuid = sd.file_main_uuid "+

            "<where>" +
            "  <if test='queryParams.deleteFlag != null'>" +
            "       AND (c.delete_flag = #{queryParams.deleteFlag} " +
            "       AND (sd.delete_flag = #{queryParams.deleteFlag} or sd.delete_flag is null)" +
            "       AND (pc.delete_flag = #{queryParams.deleteFlag} or pc.delete_flag is null)) " +
            "   </if>" +
            "   <if test='queryParams.customerPhone != null and queryParams.customerPhone != \"\"'> AND c.customer_phone =#{queryParams.customerPhone} </if>" +
            "</where>" +
            "</script>")
    public List<CustomerDetailMobileQueryPo> customerDetailMobileQuery(@Param("queryParams") CustomerDetailMobileQueryMoReq customerDetailMobileQueryMoReq);





    @Select("<script>" +
            " SELECT  " +
            " c.id as id, " +
            " c.customer_name as customerName, " +
            " c.sign_file_main_uuid as signFileMainUuid " +

            "FROM " +
            " t_customer c " +
            "<where>" +
            "  <if test='queryParams.deleteFlag != null'>" +
            "       AND c.delete_flag = #{queryParams.deleteFlag} " +
            "   </if>" +
            "   <if test='queryParams.customerPhone != null and queryParams.customerPhone != \"\"'> AND c.customer_phone =#{queryParams.customerPhone} </if>" +
            "</where>" +
            "</script>")
    public List<CustomerSignQueryPo> customerSignQuery(@Param("queryParams") CustomerSignQueryMoReq customerSignQueryMoReq);



    @Select("<script>" +
            " SELECT  " +
            " c.id as id, " +
            " c.customer_name as customerName, " +
            " c.customer_uuid as customerUuid, " +
            " c.customer_phone as customerPhone, " +
            " c.customer_wechat as customerWechat, " +
            " c.register_datetime as registerDatetime, " +
            " c.customer_type as customerType, " +
            " c.parent_customer_uuid as parentCustomerUuid, " +
            " pc.customer_name as parentCustomerName, " +
            " c.balance_number as balanceNumber, " +
            " c.gift_money as giftMoney, " +
            " c.reward_flag as rewardFlag, " +
            " c.share_code as shareCode, " +
            " c.remark as remark, " +
            " c.report_unread_number as reportUnreadNumber, " +
            " c.avatar_url as avatarUrl, " +
            " c.agent_grade as agentGrade " +

            "FROM " +
            " t_customer c " +
            " LEFT JOIN  t_customer pc ON c.parent_customer_uuid = pc.customer_uuid "+
            "<where>" +
            "   <if test='queryParams.customerName != null and queryParams.customerName != \"\"'> AND c.customer_name LIKE CONCAT('%',#{queryParams.customerName},'%') </if>" +
            "   <if test='queryParams.customerPhone != null and queryParams.customerPhone != \"\"'> AND c.customer_phone LIKE CONCAT('%',#{queryParams.customerPhone},'%') </if>" +
            "   <if test='queryParams.customerWechat != null and queryParams.customerWechat != \"\"'> AND c.customer_wechat LIKE CONCAT('%',#{queryParams.customerWechat},'%') </if>" +
            "   <if test='queryParams.shareCode != null and queryParams.shareCode != \"\"'> AND c.share_code LIKE CONCAT('%',#{queryParams.shareCode},'%') </if>" +
            "   <if test='queryParams.parentCustomerUuid != null and queryParams.parentCustomerUuid != \"\"'> AND c.parent_customer_uuid =#{queryParams.parentCustomerUuid} </if>" +
            "   <if test='queryParams.customerType != null and queryParams.customerType != \"\"'> AND c.customer_type =#{queryParams.customerType} </if>" +
            "   <if test='queryParams.agentGrade != null and queryParams.agentGrade != \"\"'> AND c.agent_grade =#{queryParams.agentGrade} </if>" +
            "  <if test='queryParams.deleteFlag != null'>" +
            "       AND (c.delete_flag = #{queryParams.deleteFlag} " +
            "       AND (pc.delete_flag = #{queryParams.deleteFlag} or pc.delete_flag is null)) " +
            "   </if>" +
            "   <if test='queryParams.registerBeginDate != null and queryParams.registerEndDate != null'> AND c.register_datetime &gt;= #{queryParams.registerBeginDate} AND c.register_datetime &lt;= #{queryParams.registerEndDate} </if>" +
            "</where>" +
            "</script>")
    public List<CustomerListQueryPo> customerListQueryNoPage(@Param("queryParams") CustomerListQueryNoPageMoReq customerListQueryNoPageMoReq);


    public int deleteTCustomerByUuids(@Param("deleteFlag") Long deleteFlag,@Param("customerUuids") String[] customerUuids);

    public int deleteTCustomerByids(@Param("deleteFlag") Long deleteFlag,@Param("ids") Long[] ids);


    public int updateAgentTCustomer(@Param("parentCustomerUuid") String parentCustomerUuid );

    public int updateAgentTCustomer2(@Param("parentCustomerUuid") String parentCustomerUuid );

    public int updateAgentTotalAgencyFee(UpdateAgentTotalAgencyFeeMoReq updateAgentTotalAgencyFeeMoReq);

    public int customerReportUnReadNumberMinusone(HashMap<String,Object> map );
    public int customerReportUnReadNumberAddone(HashMap<String,Object> map );
    public TCustomer queryCustomerReportUnReadNumber(HashMap<String,Object> map );

    public TCustomer selectCustomerByOrderCode(HashMap<String,Object> map );
}
